package com.bootx.job;

import com.bootx.service.RedisService;
import jakarta.annotation.Resource;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.util.Date;

/**
 * @author black
 */
@Component
public class CommonJob {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    private RedisService redisService;

    @Scheduled(fixedRate = 1000 * 60 * 60* 24 * 10)
    public void run (){
        redisService.delete("redPackageInfo:maxSn");
        redisService.delete("win");
        redisService.delete("fail");
        redisService.delete("win1");
        redisService.delete("fail1");

        File file = new File("D:/red/fabaorenshu");
        File file1 = new File("D:/red/redpackage");
        try {
            Path path = Paths.get("D:/red/fabaorenshu");
            BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class);
            FileTime creationTime = attr.creationTime();
            long millis = creationTime.toMillis();
            if((System.currentTimeMillis()-millis)/1000/60/60/24>1){
                FileUtils.deleteDirectory(file);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            boolean mkdir = file.mkdir();
            if(mkdir){
                System.out.println(file.getAbsolutePath()+":创建成功");
            }else{
                System.out.println(file.getAbsolutePath()+":创建失败");
            }
        }
        try {
            Path path = Paths.get("D:/red/redpackage");
            BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class);
            FileTime creationTime = attr.creationTime();
            long millis = creationTime.toMillis();
            if((System.currentTimeMillis()-millis)/1000/60/60/24>1){
                FileUtils.deleteDirectory(file1);
            }
        } catch (IOException ignored) {
        }finally {
            boolean mkdir = file1.mkdirs();
            if(mkdir){
                System.out.println(file1.getAbsolutePath()+":创建成功");
                // 再创建一个文件夹
                File file2 = new File("D:/red/redpackage/"+System.currentTimeMillis());
                boolean created = file2.mkdir();
                if (created) {
                    System.out.println("文件夹创建成功");
                } else {
                    System.out.println("文件夹创建失败");
                }
            }else{
                System.out.println(file1.getAbsolutePath()+":创建失败");
            }
        }
        //检测这两个目录在不在，不在直接抛出异常
        if(!file.exists()){
            System.out.println(file.getAbsolutePath()+"不存在");
            System.exit(1);
        }
        if(!file1.exists()){
            System.out.println(file1.getAbsolutePath()+"不存在");
            System.exit(1);
        }
    }
}
